package com.itheima.travel.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.travel.mapper.FavoriteMapper;
import com.itheima.travel.mapperExt.FavoriteMapperExt;
import com.itheima.travel.mapperExt.RouteMapperExt;
import com.itheima.travel.pojo.Favorite;
import com.itheima.travel.pojo.FavoriteExample;
import com.itheima.travel.pojo.Route;
import com.itheima.travel.req.FavoriteVo;
import com.itheima.travel.req.RouteVo;
import com.itheima.travel.req.UserVo;
import com.itheima.travel.service.FavoriteService;
import com.itheima.travel.utils.BeanConv;
import com.itheima.travel.utils.EmptyUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.List;

@Service
public class FavoriteServiceImpl implements FavoriteService {
    @Autowired
    private FavoriteMapper favoriteMapper;
    @Autowired
    private HttpSession httpSession;
    @Autowired
    private RouteMapperExt routeExtMapper;
    @Autowired
    private FavoriteMapperExt favoriteMapperExt;

    /**
     * 添加收藏方法
     *
     * @return
     */
    @Override
    public Integer addFavorite(FavoriteVo favoriteVo) {
        Object obj = httpSession.getAttribute(httpSession.getId());
        if (!EmptyUtil.isNullOrEmpty(obj)) {
            UserVo userVo = (UserVo) obj;
            favoriteVo.setUserId(userVo.getId());
            int i = favoriteMapper.insert(BeanConv.toBean(favoriteVo, Favorite.class));
            Integer integer = routeExtMapper.updateRouteCountById(favoriteVo.getRouteId());
            if (integer > 0 && i > 0) {
                return integer;
            }
        }
        return null;
    }

    /**
     * 查询收藏方法
     *
     * @return
     */
    @Override
    public PageInfo<RouteVo> findMyFavorite(int pageNum, int pageSize) {
        UserVo userVo = (UserVo) httpSession.getAttribute(httpSession.getId());
        PageHelper.startPage(pageNum, pageSize);
        FavoriteExample favoriteExample = new FavoriteExample();
        favoriteExample.createCriteria().andUserIdEqualTo(userVo.getId());
        List<Route> routeList = favoriteMapperExt.selectRouteByUserId(userVo.getId());
        PageInfo<Route> routePageInfo = new PageInfo<>(routeList);
        PageInfo<RouteVo> routeVoPageInfo = new PageInfo<>();
        //外部拷贝
        BeanConv.toBean(routePageInfo,routeVoPageInfo);
        //深层拷贝
        List<RouteVo> routeVoList = BeanConv.toBeanList(routePageInfo.getList(), RouteVo.class);
        routeVoPageInfo.setList(routeVoList);

        return routeVoPageInfo;
    }

    /**
     * 判断是否收藏
     */
    @Override
    public Boolean isFavorited(FavoriteVo favoriteVo) {
        UserVo userVo = (UserVo) httpSession.getAttribute(httpSession.getId());
        if(EmptyUtil.isNullOrEmpty(userVo)){
            return false;
        }
        FavoriteExample favoriteExample = new FavoriteExample();
        favoriteExample.createCriteria().andUserIdEqualTo(userVo.getId()).andRouteIdEqualTo(favoriteVo.getRouteId());
        List<Favorite> favoriteList = favoriteMapper.selectByExample(favoriteExample);

        return favoriteList.size()>0;
    }
}
